{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'sents_structure' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-2-b76821f45c72>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     18\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     19\u001b[0m \u001b[0mtree_data\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m\u001b[1;34m\"name\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;34m\"root\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"children\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 20\u001b[1;33m \u001b[1;32mfor\u001b[0m \u001b[0midx\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0msent\u001b[0m \u001b[1;32min\u001b[0m \u001b[0menumerate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msents_structure\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     21\u001b[0m     \u001b[0msent_data\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m\u001b[1;34m\"name\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;34m\"sentence %d\"\u001b[0m\u001b[1;33m%\u001b[0m\u001b[0midx\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"children\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     22\u001b[0m     \u001b[1;32mfor\u001b[0m \u001b[0mnode\u001b[0m \u001b[1;32min\u001b[0m \u001b[0msent\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpredictNodes\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'sents_structure' is not defined"
     ]
    }
   ],
   "source": [
    "from utils.MindGraphUtils import *\n",
    "from utils.otherUtils import IO\n",
    "from pyecharts.charts import Page, Tree\n",
    "from pyecharts import options as opts\n",
    "tokens = IO.loadJson(\"temp/data/tokens.json\")\n",
    "srl = IO.loadJson(\"temp/data/srl.json\")\n",
    "sents_srl = {}\n",
    "for item in srl:\n",
    "    sent_idx = item[\"sent_idx\"]\n",
    "    if sent_idx not in sents_srl:\n",
    "        sents_srl[sent_idx] = {\"tokens\":tokens[sent_idx],\"predicts\":[],\"args\":[]}\n",
    "    sents_srl[sent_idx][\"predicts\"].append((item[\"predict_type\"],item[\"predict_span\"]))\n",
    "    sents_srl[sent_idx][\"args\"].append(item[\"args\"])\n",
    "sent_structures = []\n",
    "for idx,sent_srl in enumerate(sents_srl.values()):\n",
    "    sent = Sent_Structure(sent_srl[\"tokens\"],sent_srl[\"predicts\"],sent_srl[\"args\"])\n",
    "    sent_structures.append(sent)\n",
    "\n",
    "tree_data = {\"name\":\"root\",\"children\":[]}\n",
    "for idx,sent in enumerate(sents_structure[:2]):\n",
    "    sent_data = {\"name\":\"sentence %d\"%idx,\"children\":[]}\n",
    "    for node in sent.predictNodes:\n",
    "        objects = {\"name\":node.value,\"children\":[{\"name\":son.value} for son in node.sons]}\n",
    "        if node.father != None:\n",
    "            relation = {\"name\":node.father.value,\"children\":[objects]}\n",
    "        else:\n",
    "            relation = objects\n",
    "        sent_data[\"children\"].append(relation)\n",
    "    tree_data[\"children\"].append(sent_data)\n",
    "tree = (Tree().add(\"演示数据\",data = [tree_data]).set_global_opts(title_opts=opts.TitleOpts(\"MindGraph\")))\n",
    "tree.render()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
